<template>
  <el-container>
    <el-header class="header-tabs">
      <el-tabs type="card" v-model="params.platform" @tab-change="tabChange">
        <el-tab-pane label="综合管理平台" :name="1"></el-tab-pane>
        <el-tab-pane label="客户端" :name="2"></el-tab-pane>
        <el-tab-pane label="移动端" :name="3"></el-tab-pane>
      </el-tabs>
    </el-header>
    <el-header>
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-input v-model="params.keywords" placeholder="菜单名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-auth="1020101" type="primary" icon="el-icon-search"  @click="handleSearch()">查询</el-button>
          <el-button v-auth="1020102" type="primary" icon="el-icon-plus"  @click="handleAdd()">新增</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="listData" v-bind:params="params" row-key="id" @selection-change="selectionChange" stripe border hidePagination expand>
        <el-table-column align="left" header-align="center" prop="title" label="菜单名称" width="280">
          <template #default="scope">
            <el-icon>
              <component :is="scope.row.icon" />
            </el-icon>
            <span>{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column label="菜单别名" prop="name" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="菜单类型" prop="menuTypeName" width="100"></el-table-column>
        <el-table-column label="隐藏/显示菜单" prop="hidden" width="120">
          <template #default="scope">
            <el-switch v-model="scope.row.hidden" @change="changeSwitch(scope.row)" :loading="scope.row.$switchHidden" :active-value="false" :inactive-value="true"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="隐藏/显示面包屑" prop="hiddenBreadcrumb" width="120">
          <template #default="scope">
            <div v-if="scope.row.hiddenBreadcrumb == true" style="color: #ff0000">
              隐藏
            </div>
            <div v-else-if="scope.row.hiddenBreadcrumb == false" style="color: #000080">
              显示
            </div>
          </template>
        </el-table-column>
        <el-table-column label="路由地址" prop="path" width="250"></el-table-column>
        <el-table-column label="视图路径" prop="component" width="250"></el-table-column>
        <el-table-column label="操作" align="left">
          <template #default="scope">
            <el-button-group>
              <el-button v-auth="1020103" text type="primary" size="small" icon="el-icon-upload" plain :disabled="scope.row.canUp != true" @click="handleUpload(scope.row, scope.$index)">上移</el-button>
              <el-button v-auth="1020104" text type="primary" size="small" icon="el-icon-download" plain :disabled="scope.row.canDown != true" @click="handleDownload(scope.row, scope.$index)">下移</el-button>
              <el-button v-auth="1020105" text type="primary" size="small" icon="el-icon-search" @click="handleShow(scope.row, scope.$index)">查看</el-button>
              <el-button v-auth="1020106" text type="primary" size="small" icon="el-icon-edit" class="btnBlue1" @click="handleEdit(scope.row, scope.$index)">编辑</el-button>
              <el-popconfirm  title="确定删除吗？" @confirm="handleRemove(scope.row, scope.$index)">
                <template #reference>
                  <el-button v-auth="1020107" type="text" size="small" icon="el-icon-remove" class="btnRed">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
  
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="callBack" @closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from "./components/save";
export default {
  name: "dept",
  components: {
    saveDialog,
  },
  data() {
    return {
      params: {
        platform: 1,
        keywords: "",
      },
      listData: this.$API.menu.getTreeList, //表格返回信息
      dialog: {
        save: false,
      },
    };
  },
  methods: {
    //获取数据
    handleSearch() {
      this.$refs.table.loadData(this.params);
    },
    tabChange() {
      this.$refs.table.loadData(this.params);
    },
    //上移动
    async handleUpload(row) {
      let par = {
        id: row.id,
        platform:row.platform,
        flag: 1,
      };
      var res = await this.$API.menu.uploadMenu.put(par);
      if (res.code == 200) {
         this.$refs.table.reloadData();
      }else{
         this.$Message.warning(res.msg);
      }
    },
    //下移动
    async handleDownload(row) {
      let par = {
        id: row.id,
        platform:row.platform,
        flag: 2,
      };
      var res = await this.$API.menu.downloadMenu.put(par);
      if (res.code == 200) {
        this.$refs.table.reloadData();
      }else{
        this.$Message.warning(res.msg);
      }
    },
    //查看
    handleShow(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("show").setData(row);
      });
    },
    //添加
    handleAdd() {
      this.dialog.save = true;
      this.$nextTick(() => {
        var par={
          platform: this.params.platform
        }
        this.$refs.saveDialog.open().getGroup(par);
      });
    },
    //编辑
    handleEdit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //表格内开关
    async changeSwitch(row) {
      row.$switchHidden = true;
      var par = {
        id: row.id,
      };
      var res = await this.$API.menu.forbidOrEnable.put(par);
      if (res.code != 200) {
        this.$Message.warning(res.msg);
      }
      delete row.$switchHidden;
    },
    //单条删除
    async handleRemove(row, index) {
      var reqData = { id: row.id };
      var res = await this.$API.menu.deleteById.delete(reqData);
      if (res.code == 200) {
        //这里需要重新获取，删除的是树结构
        this.$refs.table.reloadData();
        this.$Message.success("删除成功");
      } else {
        this.$Message.warning(res.msg);
      }
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
    //本地更新数据
    callBack(data, mode) {
      this.$refs.table.reloadData();
      // if (mode == "add") {
      //   this.$refs.table.reloadData();
      // } else if (mode == "edit") {
      //   this.$refs.table.tableData
      //     .filter((item) => item.id === data.id)
      //     .forEach((item) => {
      //       Object.assign(item, data);
      //     });
      // }
    },
  },
};
</script>

<style></style>
